//
//  ConnectViewController.swift
//  Keyboard
//
//  Created by xinglian on 2022/12/18.
//

import UIKit
import TCCKit
class ConnectViewController: BaseViewController {
    @IBOutlet var bgView: UIView!
    @IBOutlet var table: UITableView!
    @IBOutlet var nodataImg: UIImageView!
    @IBOutlet var nodataTips: UILabel!
    @IBOutlet var nodateBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    deinit{
        NotificationCenter.default.removeObserver(self)
    }
    
    override func initUI() {
        _ = self.bgView.shadow()
        self.perform(#selector(actionForBtn), with: self.nodateBtn, afterDelay: 0.5)
        self.table.register(ConnectCell.nib(), forCellReuseIdentifier: ConnectCell.ID())
        
        self.table.setupRefreshing()
        self.table.updateBlock = {
            BLE.startScan()
        }
        BLE.scanBlock = {[weak self] in
            CCHud.start("", stopTime: 1)
            self?.table.reloadData()
            self?.table.endRefreshing()
            self?.hiddenNodata(BLE.searchedDevices.count > 0)
        }
    }
    
    func hiddenNodata(_ hidden:Bool){
        self.nodataImg.isHidden = hidden
        self.nodataTips.isHidden = hidden
        self.nodateBtn.isHidden = hidden
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        BLE.startScan()
        CCHud.start(LS("搜索中..."))
    }
}
extension ConnectViewController{
    override func addNotify() {
        NotificationCenter.default.addObserver(self, selector: #selector(actionForBLEStatusUpdate), name: NSNotification.Name(BLEStatusUpdateNotifyName), object: nil)
    }
    
    @objc func actionForBLEStatusUpdate(){
        switch BLE.status{
        case .connectedSucces:
            CCHud.start(LS("连接成功"), stopTime: 2)
            self.table.reloadData()
        case .disconnect,.connectedFail:
            self.table.reloadData()
        default:
            break
        }
    }
}
//MARK: - UITableViewDataSource
extension ConnectViewController
{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return BLE.searchedDevices.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ConnectCell.ID()) as! ConnectCell
        cell.periphral = BLE.searchedDevices[indexPath.row]
        cell.setup()
        cell.block = { need in
            if need{
                CCHud.start(LS("连接中..."))
                BLE.connect(BLE.searchedDevices[indexPath.row])
            }else{
                BLE.disConnect(BLE.searchedDevices[indexPath.row])
            }
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension ConnectViewController
{
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
